home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / y2l.lha / y2l / yacc.lalr < prev    next >
Text File  |  1992-08-20  |  4KB  |  131 lines

  1. GLOBAL {
  2. typedef union { tScanAttribute Scan; } tParsAttribute;
  3.  
  4. int rword;        /* 0 = %token, 1 = %oper, 2 = %type */
  5. tStringRef lhs, tag, prec;
  6. tStringRef left_str, right_str, none_str, colon_str, bar_str, action_str, prec_str, dot_str;
  7. int has_rhs, has_action;
  8. struct tCell default_action = { 0, 0 };
  9. }
  10.  
  11. BEGIN {
  12. left_str    = PutString ("\n\tLEFT", 6);
  13. right_str    = PutString ("\n\tRIGHT", 7);
  14. none_str    = PutString ("\n\tNONE", 6);
  15. colon_str    = PutString ("\t:", 2);
  16. bar_str        = PutString ("\t|", 2);
  17. action_str    = PutString (" }}", 3);
  18. prec_str    = PutString ("PREC", 4);
  19. dot_str        = PutString ("\n\t.", 3);
  20. default_action.car = action_str;
  21. }
  22.  
  23. TOKEN
  24.     identifier    = 1
  25.     C_IDENT        = 2
  26.     number        = 3
  27.     '%left'        = 4
  28.     '%right'    = 5
  29.     '%nonassoc'    = 6
  30.     '%token'    = 7
  31.     '%prec'        = 8
  32.     '%type'        = 9
  33.     '%start'    = 10
  34.     '%union'    = 11
  35.     '%%'        = 12
  36.     '<'        = 13
  37.     '>'        = 14
  38.     ','        = 15
  39.     '|'        = 16
  40.     '{'        = 17
  41.     '}'        = 18
  42.     ';'        = 19
  43.     ':'        = 20
  44.  
  45. RULE    /* the grammar is taken from the yacc documentation */
  46.  
  47. spec    : def * '%%' { put_text (text_list); } rules { yacc_globals (); } [ '%%' ]
  48.     .
  49.  
  50. def    : '%start' identifier { start_symbol = $2.Scan.string; }
  51.     | '%union'
  52.     | rword tag nlist
  53.     .
  54.  
  55. rword    : '%token'    { rword = 0; }
  56.     | '%left'    { rword = 1; put_oper (left_str); }
  57.     | '%right'    { rword = 1; put_oper (right_str); }
  58.     | '%nonassoc'    { rword = 1; put_oper (none_str); }
  59.     | '%type'    { rword = 2; type = 1; }
  60.     .
  61.  
  62. tag    : '<' identifier '>' { tag = $2.Scan.string; }
  63.     | { tag = 0; }
  64.     .
  65.  
  66. nlist    : nmno
  67.     | nlist nmno
  68.     | nlist ',' nmno
  69.     .
  70.  
  71. nmno    : identifier { switch (rword) {
  72.              case 1 : put_oper ($1.Scan.string);
  73.              case 0 : put_token ($1.Scan.string, 0); break;
  74.              default: break;
  75.                }
  76.                if (tag) put_type ($1.Scan.string, tag); }
  77.     | identifier number { switch (rword) {
  78.                     case 1 : put_oper ($1.Scan.string);
  79.                     case 0 : put_token ($1.Scan.string, $2.Scan.number); break;
  80.                     default: break;
  81.                   }
  82.                   if (tag) put_type ($1.Scan.string, tag); }
  83.     .
  84.  
  85. rules    : C_IDENT
  86.       { rule_elmts_list = prec = has_rhs = has_action = 0; lhs = $0.Scan.string;
  87.         put_lhs (lhs); put_delim (lhs); put_delim (colon_str); }
  88.       ':' rbody prec
  89.       { if (has_rhs && ! has_action) put_whole_action (& default_action, true);
  90.         if (prec) { put_delim (prec_str); put_delim (prec); }
  91.         if (yyTerminal != 16) put_delim (dot_str);
  92.         put_rule (rule_elmts_list); put_text (text_list); }
  93.     | rules rule
  94.     .
  95.  
  96. rule    : C_IDENT
  97.       { rule_elmts_list = prec = has_rhs = has_action = 0; lhs = $0.Scan.string;
  98.         put_lhs (lhs); put_delim (lhs); put_delim (colon_str); }
  99.       ':' rbody prec
  100.       { if (has_rhs && ! has_action) put_whole_action (& default_action, true);
  101.         if (prec) { put_delim (prec_str); put_delim (prec); }
  102.         if (yyTerminal != 16) put_delim (dot_str);
  103.         put_rule (rule_elmts_list); put_text (text_list); }
  104.     | '|'
  105.       { rule_elmts_list = prec = has_rhs = has_action = 0;
  106.         put_lhs (lhs); put_delim (bar_str); }
  107.       rbody prec
  108.       { if (has_rhs && ! has_action) put_whole_action (& default_action, true);
  109.         if (prec) { put_delim (prec_str); put_delim (prec); }
  110.         if (yyTerminal != 16) put_delim (dot_str);
  111.         put_rule (rule_elmts_list); put_text (text_list); }
  112.     .
  113.  
  114. rbody    : rbody identifier { put_symbol ($2.Scan.string); has_rhs = 1; }
  115.     | rbody act
  116.     |
  117.     .
  118.  
  119. (* actions are delivered in 2 tokens for the purpose of synchronisation *)
  120.  
  121. act    : '{' '}' { has_action = 1; put_whole_action ($2.Scan.action, has_rhs &
  122.             /* is it last action ? */
  123.             (yyTerminal == 16 || yyTerminal == 19 || yyTerminal == 0)); }
  124.     .
  125.  
  126. prec    : '%prec' identifier     { prec = $2.Scan.string; }
  127.     | '%prec' identifier act { prec = $2.Scan.string; }
  128.     | prec ';'
  129.     |
  130.     .
  131.